<?php

namespace App\Repositories\Eloquent\Edu;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

use App\Repositories\Eloquent\Mgt\UserRepository as User;

class TermRepository extends Repository
{

    protected $user;

    public function __construct(App $app, User $user)
    {
        parent::__construct($app);

        $this->user = $user;
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Edu\Term';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $result = $this->model
                ->with('type', 'branch', 'categories', 'clases', 'users',
                    'categories.clases',
                    'categories.clases.users')
                ->orderBy('id', 'desc');

        return $result;
    }

    /**
     * 发布报名时，过滤掉班级为空的培训计划
     * @return [type] [description]
     */
    public function deploy()
    {
        $result = $this->model
                ->with('type', 'branch', 'categories', 'clases', 'users',
                    'categories.clases',
                    'categories.clases.users')
                ->has('clases','>',0)
                ->orderBy('id', 'desc')->get();

        return $result;
    }

    /**
     * 选出公益培训类的培训计划
     * @return [type] [description]
     */
    public function publish()
    {
        $result = $this->model->training()
                ->with('type', 'branch', 'categories', 'clases', 'users',
                    'categories.clases',
                    'categories.clases.users')
                ->orderBy('id', 'desc')->get();

        return $result;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request)
    {
        $result = $this->create([
            'type_id' => $request->type,
            'branch_id' => $request->branch,
            'name' => $request->name,
        ]);

        if( !!$result )
            return $this->responseSuccess('培训计划添加成功');

        return $this->responseFail(500, '培训计划添加失败');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($termId)
    {
        $term = $this->model
            ->with('categories','clases', 'users')
            ->find($termId);

        if (!$term) {
            return $this->responseFail(404,'培训计划信息不正确');
        }

        return $term;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function renew($request, $id)
    {
        $result = $this->update([
            'type_id' => $request->type,
            'branch_id' => $request->branch,
            'name' => $request->name,
        ], $id);

        if(!!$result)
            return $this->responseSuccess('修改成功');

        return $this->responseFail(500,'修改失败');
    }


    public function export($id)
    {
        //$term = $this->find($id);


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}